package com.ruoyi.evalution.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.evalution.domain.LabEvalutionTopic;
import com.ruoyi.evalution.domain.LabEvalutionTopicTask;
import com.ruoyi.evalution.dto.LabScoreDto;
import com.ruoyi.evalution.service.ILabEvalutionTopicService;
import com.ruoyi.evalution.service.ILabEvalutionTopicTaskService;

/**
 * 实验室评测Controller
 * 
 * @author winner pan
 * @date 2022-08-03
 */
@RestController
@RequestMapping("/evalution/topic")
public class LabEvalutionTopicController extends BaseController {
	@Autowired
	private ILabEvalutionTopicService labEvalutionTopicService;
	@Autowired
	private ILabEvalutionTopicTaskService labEvalutionTopicTaskService;
	/**
	 * 查询实验室评测列表
	 */
	@PreAuthorize("@ss.hasPermi('evalution.topic.query')")
	@GetMapping("/list")
	public TableDataInfo list(LabEvalutionTopic labEvalutionTopic) {
		startPage();
		List<LabEvalutionTopic> list = labEvalutionTopicService.selectLabEvalutionTopicList(labEvalutionTopic);
		return getDataTable(list);
	}

	/**
	 * 导出实验室评测列表
	 */
	//@PreAuthorize("@ss.hasPermi('evalution:topic:export')")
	@Log(title = "实验室评测", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(Long instanceId) {
		LabEvalutionTopicTask query = new LabEvalutionTopicTask();
		query.setInstanceId(instanceId);
		List<LabEvalutionTopicTask> taskList = labEvalutionTopicTaskService.selectLabEvalutionTopicTaskList(query);
		List<LabScoreDto> scoreList = new ArrayList<LabScoreDto>();
		Map<String, LabEvalutionTopicTask> taskMap = new HashMap<String, LabEvalutionTopicTask>();
		taskList.forEach(item->{
			if(item.getScoreType()==2){
				taskMap.put(item.getInstanceId()+"_"+item.getOrgId(), item);
			}
		});
		taskList.forEach(item->{
			if(item.getScoreType()==1){
				LabScoreDto scoreDto = new LabScoreDto();
				BigDecimal totalScore1 = new BigDecimal("0");
				scoreDto.setBigDeviceScore1(item.getBigDeviceScore());
				totalScore1 = addScore(totalScore1,item.getBigDeviceScore());
				scoreDto.setDataReportScore1(item.getDataReportScore());
				totalScore1 = addScore(totalScore1,item.getDataReportScore());
				scoreDto.setDevicesUsingScore1(item.getDevicesUsingScore());
				totalScore1 = addScore(totalScore1,item.getDevicesUsingScore());
				scoreDto.setForeignExchangesScore1(item.getForeignExchangesScore());
				totalScore1 = addScore(totalScore1,item.getForeignExchangesScore());
				scoreDto.setInformationScore1(item.getInformationScore());
				totalScore1 = addScore(totalScore1,item.getInformationScore());
				scoreDto.setLabName(item.getOrgName());
				scoreDto.setLabRuningScore1(item.getLabRuningScore());
				totalScore1 = addScore(totalScore1,item.getLabRuningScore());
				scoreDto.setLabUsingScore1(item.getLabUsingScore());
				totalScore1 = addScore(totalScore1,item.getLabUsingScore());
				scoreDto.setOpenProjectScore1(item.getOpenProjectScore());
				totalScore1 = addScore(totalScore1,item.getOpenProjectScore());
				scoreDto.setSchoolEnterpriseScore1(item.getSchoolEnterpriseScore());
				totalScore1 = addScore(totalScore1,item.getSchoolEnterpriseScore());
				scoreDto.setSecurityScore1(item.getSecurityScore());
				totalScore1 = addScore(totalScore1,item.getSecurityScore());
				scoreDto.setTeachingResourceScore1(item.getTeachingResourceScore());
				totalScore1 = addScore(totalScore1,item.getTeachingResourceScore());
				scoreDto.setTeachingScore1(item.getTeachingScore());
				totalScore1 = addScore(totalScore1,item.getTeachingScore());
				scoreDto.setTeamBuildScore1(item.getTeamBuildScore());
				totalScore1 = addScore(totalScore1,item.getTeamBuildScore());
				scoreDto.setTeamResultScore1(item.getTeamResultScore());
				totalScore1 = addScore(totalScore1,item.getTeamResultScore());
				scoreDto.setTotalScore1(totalScore1);
				LabEvalutionTopicTask schoolSCore = taskMap.get(item.getInstanceId()+"_"+item.getOrgId());
				if(schoolSCore!=null){
					BigDecimal totalScore = new BigDecimal("0");
					scoreDto.setBigDeviceScore(item.getBigDeviceScore());
					totalScore = addScore(totalScore,schoolSCore.getBigDeviceScore());
					scoreDto.setDataReportScore(schoolSCore.getDataReportScore());
					totalScore = addScore(totalScore,schoolSCore.getDataReportScore());
					scoreDto.setDevicesUsingScore(schoolSCore.getDevicesUsingScore());
					totalScore = addScore(totalScore,schoolSCore.getDevicesUsingScore());
					scoreDto.setForeignExchangesScore(schoolSCore.getForeignExchangesScore());
					totalScore = addScore(totalScore,schoolSCore.getForeignExchangesScore());
					scoreDto.setInformationScore(schoolSCore.getInformationScore());
					totalScore = addScore(totalScore,schoolSCore.getInformationScore());
					scoreDto.setLabName(schoolSCore.getOrgName());
					scoreDto.setLabRuningScore(schoolSCore.getLabRuningScore());
					totalScore = addScore(totalScore,schoolSCore.getLabRuningScore());
					scoreDto.setLabUsingScore(schoolSCore.getLabUsingScore());
					totalScore = addScore(totalScore,schoolSCore.getLabUsingScore());
					scoreDto.setOpenProjectScore(schoolSCore.getOpenProjectScore());
					totalScore = addScore(totalScore,schoolSCore.getOpenProjectScore());
					scoreDto.setSchoolEnterpriseScore(schoolSCore.getSchoolEnterpriseScore());
					totalScore = addScore(totalScore,schoolSCore.getSchoolEnterpriseScore());
					scoreDto.setSecurityScore(schoolSCore.getSecurityScore());
					totalScore = addScore(totalScore,schoolSCore.getSecurityScore());
					scoreDto.setTeachingResourceScore(schoolSCore.getTeachingResourceScore());
					totalScore = addScore(totalScore,schoolSCore.getTeachingResourceScore());
					scoreDto.setTeachingScore(schoolSCore.getTeachingScore());
					totalScore = addScore(totalScore,schoolSCore.getTeachingScore());
					scoreDto.setTeamBuildScore(schoolSCore.getTeamBuildScore());
					totalScore = addScore(totalScore,schoolSCore.getTeamBuildScore());
					scoreDto.setTeamResultScore(schoolSCore.getTeamResultScore());
					totalScore = addScore(totalScore,schoolSCore.getTeamResultScore());
					scoreDto.setTotalScore(totalScore);
				}
				scoreList.add(scoreDto);
			}
		});
		ExcelUtil<LabScoreDto> util = new ExcelUtil<LabScoreDto>(LabScoreDto.class);
		return util.exportExcel(scoreList, "topic");
	}
	
	private BigDecimal addScore(BigDecimal totalScore, BigDecimal item){
		if(item!=null){
			return totalScore.add(item);
		}
		return totalScore;
	}

	/**
	 * 获取实验室评测详细信息
	 */
	//@PreAuthorize("@ss.hasPermi('evalution:topic:query')")
	@GetMapping(value = "/{instanceId}")
	public AjaxResult getInfo(@PathVariable("instanceId") Long instanceId) {
		return AjaxResult.success(labEvalutionTopicService.selectLabEvalutionTopicById(instanceId));
	}

	/**
	 * 新增实验室评测
	 */
	@PreAuthorize("@ss.hasPermi('evalution:topic:edit')")
	@Log(title = "实验室评测", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody LabEvalutionTopic labEvalutionTopic) {
		labEvalutionTopic.setCreateBy(SecurityUtils.getUsername());
		labEvalutionTopic.setStatus(Integer.valueOf(0));
		int ret = labEvalutionTopicService.insertLabEvalutionTopic(labEvalutionTopic);
		if(ret==0){
			return AjaxResult.error("保存失败");
		}
		return AjaxResult.success(labEvalutionTopic);
	}

	/**
	 * 修改实验室评测
	 */
	@PreAuthorize("@ss.hasPermi('evalution:topic:edit')")
	@Log(title = "实验室评测", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody LabEvalutionTopic labEvalutionTopic) {
		int ret = labEvalutionTopicService.updateLabEvalutionTopic(labEvalutionTopic);
		if(ret==0){
			return AjaxResult.error("保存失败");
		}
		return AjaxResult.success(labEvalutionTopic);
	}

	/**
	 * 删除实验室评测
	 */
	@PreAuthorize("@ss.hasPermi('evalution:topic:edit')")
	@Log(title = "实验室评测", businessType = BusinessType.DELETE)
	@DeleteMapping("/{instanceId}")
	public AjaxResult remove(@PathVariable Long instanceId) {
		return toAjax(labEvalutionTopicService.deleteLabEvalutionTopicById(instanceId));
	}
}
